<template>
  <div class="address">
    <van-address-edit :show-area="false" :show-postal="false" :address-info="addressVal"
      :show-delete="showDel" show-set-default @save="onSave" @delete="onDelete"
      @change-detail="onChangeDetail" />
  </div>
</template>

<script>
  import Vue from "vue";
  import { AddressEdit } from "vant";
  import apiMar from "@/api/api";
  Vue.use(AddressEdit);
  export default {
    components: {},
    data() {
      return {
        searchResult: [],
        showDel: false,
        addressVal: {}
      };
    },
    computed: {},
    watch: {},
    methods: {
      onSave(val) {
        console.log(val, "savesavesavesavesavesavesavesavesavesave");
        let params = {
          name: val.name,
          phone: val.tel,
          detail: val.addressDetail,
          default: val.isDefault ? 1 : 0
        };
        if (this.$route.query.edit) {
          params.id = this.addressVal.id
          apiMar
            .updateAddress(params)
            .then(res => {
              this.$router.push({
                path: "/address"
              });
            })
            .catch(err => {});
        } else {
          apiMar
            .createAddress(params)
            .then(res => {
              this.$router.push({
                path: "/address"
              });
            })
            .catch(err => {
              // this.$Message.error(err)
            });
        }

      },
      onDelete(val) {
        console.log(val, '删除');
        let params = {
          id: val.id
        }
        apiMar
          .deleteAddress(params)
          .then(res => {
            this.$router.push({
              path: "/address"
            });
          })
          .catch(err => {
            // this.$Message.error(err)
          });
      },
      onChangeDetail(val) {
        if (val) {
          this.searchResult = [
          {
            name: "黄龙万科中心",
            address: "杭州市西湖区"
          }];
        } else {
          this.searchResult = [];
        }
      }
    },
    beforeDestroy() {
      this.$tool.clearStorage(['addressVal'])
    },
    created() {
      // let addressVal = this.$route.query.val;
      // this.AddressInfo.name = addressVal.name;
      // this.AddressInfo.tel = addressVal.tel;
      // this.AddressInfo.province = "浙江省";

      if (this.$route.query.edit) {
        this.showDel = true
        this.addressVal = this.$tool.getlocalStorage("addressVal") ? JSON.parse(this.$tool
          .getlocalStorage("addressVal")) : '';
        this.addressVal.addressDetail = this.addressVal.detail
        this.addressVal.isDefault = this.addressVal.default == 1 ? true : false

        console.log(this.addressVal);
      }


    },
    mounted() {}
  };
</script>
<style lang="scss" scoped></style>